﻿Public Class FrmUserOrg
    Private FOid As Integer
    Private Sub FrmUserOrg_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GetOrgList()
    End Sub
    Private Sub GetOrgList()
        Try
            FOid = 0
            DataGridView1.DataSource = Nothing
            Dim ErrString As String = String.Empty
            Dim ds As New DataSet
            Dim TmpArr As Byte() = Nothing
            If SControl._BASE_Org_GetOrgList(UInfo.FSession, "List", TmpArr, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            If FControl.DsRestore(ds, TmpArr) = False Then
                MsgBox("无法解析返回数据")
                Return
            End If
            DataGridView1.DataSource = ds.Tables(0).DefaultView

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text.Trim = String.Empty Then
            MsgBox("必须输入单位名称")
            Return
        End If
        Try
            Dim ErrString As String = String.Empty
            If SControl._BASE_Org_AddNew(UInfo.FSession, TextBox1.Text.Trim, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            TextBox1.Text = String.Empty
            GetOrgList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If FOid = 0 Then
            MsgBox("必须先选择一个要修改的单位")
            Return
        End If
        If TextBox1.Text.Trim = String.Empty Then
            MsgBox("单位名称必须填写!")
            Return
        End If
        Try
            Dim ErrString As String = String.Empty
            If SControl._BASE_Org_ModifyOrg(UInfo.FSession, FOid, TextBox1.Text.Trim, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            TextBox1.Text = Nothing
            GetOrgList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If FOid = 0 Then
            MsgBox("必须选择一个要删除的单位!")
            Return
        End If
        Try
            Dim ErrString As String = String.Empty
            If SControl._BASE_Org_DelOrg(UInfo.FSession, FOid, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            TextBox1.Text = Nothing
            GetOrgList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dispose()
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Try
            If DataGridView1.SelectedCells.Count < 1 Then
                Return
            End If
            FOid = DataGridView1.Item(0, e.RowIndex).Value
            If FOid = 0 Or FOid = Nothing Then
                Return
            End If
            TextBox1.Text = DataGridView1.Item(1, e.RowIndex).Value
        Catch ex As Exception
            FOid = Nothing
            MsgBox(ex.Message)
        End Try
    End Sub
End Class